package mipo.top.base.util;

import mipo.top.admin.domain.Shop;
import mipo.top.base.Dto.EmailDto;
import mipo.top.base.Dto.EmailRegisterDto;
import mipo.top.base.constant.RegisterConstant;
import mipo.top.base.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.concurrent.TimeUnit;

@Component
public class SimpleMailMsg {

    private static JavaMailSender javaMailSender;
    private static RedisTemplate redisTemplate;
    @Autowired
    public void setJavaMailSender(JavaMailSender javaMailSender) {
        SimpleMailMsg.javaMailSender = javaMailSender;
    }
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        SimpleMailMsg.redisTemplate = redisTemplate;
    }
    public static void pass(String Emailbox, Shop shop) throws Exception {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2430618938@qq.com");
        //设置接收邮箱
        helper.setTo(Emailbox);
        helper.setSubject("审核结果通知");
        // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        if (shop.getState()==1) {
            helper.setText("<h1>审核通过</h1>" +
                    "点击链接以激活店铺" +
                    "http://127.0.0.1:8081/shop/active/" + shop.getId(), true);
        }else if(shop.getState()==-1){
            helper.setText("<h1>审核驳回</h1>" +
                    "有非法字段或是图片违规，请重新申请店铺入住"+
                    "http://127.0.0.1:8081/#/RegisterAgain/?id="+ shop.getId() ,true);
        }
        // 4.发送邮件
        javaMailSender.send(mimeMessage);
    }
    public static void emailRegister(EmailRegisterDto dto) throws Exception {
        String emailCodeKey = RegisterConstant.EMAILREGISTERKEY+dto.getEmail();
        String s = (String) redisTemplate.opsForValue().get(emailCodeKey);
        String emailCode="";
        if (s==null){
            emailCode=StrUtils.getRandomString(4);
        }else {
            String[] strings = s.split(":");
            Long oldtime = Long.valueOf(strings[1]);
            Long nowtime = System.currentTimeMillis();
            if(nowtime-oldtime<=60*1000){
                throw new BusinessException("两次验证码重复发送时间不得小于60秒");
            }
            emailCode=strings[0];
        }
        String emailCodeValue = emailCode+":"+System.currentTimeMillis();
        redisTemplate.opsForValue().set(emailCodeKey,emailCodeValue,5,TimeUnit.MINUTES);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2430618938@qq.com");
        //设置接收邮箱
        helper.setTo(dto.getEmail());
        helper.setSubject("邮箱注册");
        // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        helper.setText("您的邮箱验证码是" +
                        emailCode+
                        "请在5分钟内使用"
                , true);
        javaMailSender.send(mimeMessage);
    }

}
